#include "vidhub.h"
#include "./ui_vidhub.h"
#include "model/datacenter.h"
#include <QGraphicsDropShadowEffect>
#include <QDebug>
#include "util.h"
#include "pageswitchbutton.h"

VidHub::VidHub(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::VidHub)
{
    ui->setupUi(this);

    initUI();

    connectSigalsAndSlots();
}

VidHub::~VidHub()
{
    delete ui;
}


VidHub* VidHub::instance = nullptr;
VidHub *VidHub::getInstance()
{
    if(instance == nullptr)
        instance = new VidHub();
    return instance;
}

void VidHub::initUI()
{
    // 设置窗口无边框
    this->setWindowFlags(Qt::WindowType::FramelessWindowHint);


    // 设置窗口图标
    this->setWindowIcon(QIcon(":/images/bitPlayer.ico"));

    // 添加阴影效果
    // 1. 设置窗口背景为透明
    this->setAttribute(Qt::WA_TranslucentBackground);

    // 2. 设置阴影效果
    QGraphicsDropShadowEffect* shadow = new QGraphicsDropShadowEffect(this);
    shadow->setColor(Qt::black);
    shadow->setBlurRadius(15);
    shadow->setOffset(0,0);
    ui->playerBg->setGraphicsEffect(shadow);

    // 设置body左侧按钮样式
    ui->homePageBtn->setTextColor("#000000");
    ui->homePageBtn->setImageAndText(":/images/homePage/shouyexuan.png", "首页", HomePage);
    ui->myPageBtn->setTextColor("#999999");
    ui->myPageBtn->setImageAndText(":/images/homePage/wode.png", "我的", MyselfPage);
    ui->sysPageBtn->setTextColor("#999999");
    ui->sysPageBtn->setImageAndText(":/images/homePage/admin.png", "系统", AdminPage);

    // 默认显示 "首页"
    ui->stackedWidget->setCurrentIndex(HomePage);

    // 如果用户不是管理员身份 不显示系统界面
    auto dataCenter = model::DataCenter::getInstance();
    auto myselfInfo = dataCenter->getMyselfInfo();
    if(myselfInfo->isBUser()) {
        ui->sysPageBtn->show();
    } else {
        ui->sysPageBtn->hide();
    }
}

void VidHub::connectSigalsAndSlots()
{
    // 处理 "缩小"按钮
    connect(ui->minBtn, &QPushButton::clicked, this, [=](){
        this->showMinimized();
    });
    // 处理 "取消"按钮
    connect(ui->quitBtn, &QPushButton::clicked, this, [=](){
        // 将session信息保存在文件中
        auto dataCenter = model::DataCenter::getInstance();
        dataCenter->saveDataFile();
        this->close();
    });
    //处理 "首页" "我的" "系统" "上传视频" 界面切换效果
    connect(ui->homePageBtn, &PageSwitchButton::switchPage, this, &VidHub::onSwitchPage);
    connect(ui->myPageBtn, &PageSwitchButton::switchPage, this, &VidHub::onSwitchPage);
    connect(ui->sysPageBtn, &PageSwitchButton::switchPage, this, &VidHub::onSwitchPage);
    connect(ui->myPage, &MyselfWidget::switchUploadVideoPage, this, [=](int pageIndex, const QString& videoPath){
        LOG() << "由我的页面切换到上传视频页面";
        ui->uploadVideoPage->setVideoTittle(videoPath);
        this->onSwitchPage(pageIndex);
    });
    connect(ui->uploadVideoPage, &UploadVideoPage::switchMySelfWidgetPage, [=](int pageIndex){
        LOG() << "由上传视频页面切换到我的界面";
        this->onSwitchPage(pageIndex);
    });
}

void VidHub::mousePressEvent(QMouseEvent *event)
{
    // 获取鼠标点击时的位置
    QPoint point = event->position().toPoint();
    // 检测是否在head中 & 是鼠标左键点击
    if(ui->head->geometry().contains(point))
    {
        if(event->button() == Qt::LeftButton)
        {
            // 鼠标的全局坐标 - 窗口的全局坐标 = 相对距离
            dragPos = event->globalPosition().toPoint() - this->geometry().topLeft();
            return ;
        }
    }

    QWidget::mousePressEvent(event);
}

void VidHub::mouseMoveEvent(QMouseEvent *event)
{
    // 获取鼠标点击时的位置
    QPoint point = event->position().toPoint();
    // 检测是否在head中 & 是鼠标左键点击
    if(ui->head->geometry().contains(point))
    {
        if(event->buttons() == Qt::LeftButton)
        {
            // 鼠标的全局坐标 - 相对距离 = 窗口的全局坐标
            this->move(event->globalPosition().toPoint() - dragPos);
            return ;
        }
    }

    QWidget::mouseMoveEvent(event);
}

void VidHub::resetswitchBtnInfo(int pageId)
{
    QList<PageSwitchButton*> switchBtns = this->findChildren<PageSwitchButton*>();

    for(auto& btn : switchBtns)
    {
        if(btn->getPageId() != pageId)
        {
            btn->setTextColor("#999999");
        }
        else
        {
            btn->setTextColor("#000000");
        }
    }

    if(pageId == HomePage)
    {
        // 在HomePageBtn的图片设置为高亮
        // 将MyselfPageBtn、AdminPageBtn的图片设置为灰色
        ui->homePageBtn->setImage(":/images/homePage/shouyexuan.png");
        ui->myPageBtn->setImage(":/images/homePage/wode.png");
        ui->sysPageBtn->setImage(":/images/homePage/admin");
    }
    else if(pageId == MyselfPage)
    {
        ui->homePageBtn->setImage(":/images/homePage/shouye.png");
        ui->myPageBtn->setImage(":/images/homePage/wodexuan.png");
        ui->sysPageBtn->setImage(":/images/homePage/admin");
    }
    else if(pageId == AdminPage)
    {
        ui->homePageBtn->setImage(":/images/homePage/shouye.png");
        ui->myPageBtn->setImage(":/images/homePage/wode.png");
        ui->sysPageBtn->setImage(":/images/homePage/adminxuan");
    }
    else
    {
        LOG() <<"暂不支持的页面";
    }
}

void VidHub::onSwitchPageUI(int pageId)
{
    ui->stackedWidget->setCurrentIndex(pageId);
    resetswitchBtnInfo(pageId);
}

void VidHub::showSystemPageBtn(bool isShow)
{
    if(isShow){
        ui->sysPageBtn->show();
    }else{
        ui->sysPageBtn->hide();
    }
}

void VidHub::switchToUserInfoPage(const QString &userId)
{
    onSwitchPageUI(MyselfPage);

    // 加载其他用户的个人信息
    ui->myPage->loadOtherUser(userId);
}

void VidHub::onSwitchPage(int pageId)
{
    onSwitchPageUI(pageId);

    // 如果pageId是我的页面，需要加载个人信息
    if(pageId == MyselfPage) {
        ui->myPage->loadMyself();
    }
}
